package com.fhd.dao;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

//基础公用工具类
public class BaseDao {
    private static final String driver;
    private static final String url;
    private static final String username;
    private static final String password;

    // 静态代码块，在类加载时通过类加载器从资源中读取信息
    static {
        Properties properties = new Properties();
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");

        try {
            properties.load(is);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    //获取数据库连接
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    //公共查询类
    public static ResultSet executeQuery(Connection conn, PreparedStatement pstmt,ResultSet rs,String sql,Object[] parameters) {
        try {
            // 预编译sql
            pstmt = conn.prepareStatement(sql);
            // 往sql里面放参数
            for (int i = 0;i < parameters.length;i ++){
                pstmt.setObject(i+1,parameters[i]); // 占位符和索引差1
            }
            // 获得执行对象
            rs = pstmt.executeQuery();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return rs;
    }

    //公共增删改类
    public static int executeUpdate(Connection conn, PreparedStatement pstmt,String sql,Object[] parameters) {
        int affectedRow = 0;
        try {
            // 预编译sql
            pstmt = conn.prepareStatement(sql);
            // 往sql里面放参数
            for (int i = 0;i < parameters.length;i ++){
                pstmt.setObject(i+1,parameters[i]); // 占位符和索引差1
            }
            //执行
            affectedRow = pstmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return affectedRow;
    }

    //释放资源类
    public static boolean close(Connection conn,PreparedStatement pstmt,ResultSet rs) {
        // 判断释放资源过程有没有问题
        boolean flag = true;
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                flag = false;
                throw new RuntimeException(e);
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (Exception e) {
                flag = false;
                throw new RuntimeException(e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                flag = false;
                throw new RuntimeException(e);
            }
        }
        return flag;
    }

}
